package com.da.javatest.leetCode;

import java.util.Stack;

/**
 * @author chenlida
 * @date 2019/2/25 15:03
 * @description
 */
public class Code20 {
    public static void main(String[] args) {
        System.out.println(new Code20().isValid(""));
    }

    //public boolean isValid(String s) {
    //
    //    while (true) {
    //        int length = s.length();
    //        s = s.replace("()", "").replace("{}", "").replace("[]", "");
    //        if (s.length() == 0) {
    //            return true;
    //        }
    //        if (s.length() == length) {
    //            return false;
    //        }
    //    }
    //}
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if (c == '{' || c == '(' || c == '[') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                Character c2 = stack.pop();
                if (c == '}' && c2 != '{') {
                    return false;
                }
                if (c == ')' && c2 != '(') {
                    return false;
                }
                if (c == ']' && c2 != '[') {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
